import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:zy_pos_demo/main.dart';
import 'package:zy_zhihu_demo/main.dart';
import 'package:zylearnfluttermodule/learnmain.dart';

void main() => runApp(MyApp());

Widget _widgetForRoute(String route) {
  switch (route) {
    case 'route1':
      return MyHomePage(title: 'Flutter Demo Home Page1');
    case 'route2':
      return MyHomePage(title: 'Flutter Demo Home Page2');
    default:
      return MyHomePage(title: 'Flutter Demo Home Page2');
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: _widgetForRoute(window.defaultRouteName),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class AA extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    throw UnimplementedError();
  }
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0; //flutter自己的变量

  void _incrementCounter() {
    //flutter自己的方法
    setState(() {
      _counter++;
    });
  }

  //用于从Android原生获取参数
  static const fromAndroid =
      const EventChannel('com.litngzhe.toflutter/plugin');
  StreamSubscription _fromAndroid;
  var _fromAndroidParams;

  void _startFromAndroidPlugin() {
    if (_fromAndroid == null) {
      _fromAndroid = fromAndroid
          .receiveBroadcastStream()
          .listen(_onFromAndroidEvent, onError: _onFromAndroidError);
    }
  }

  void _onFromAndroidEvent(dynamic event) {
    setState(() {
      _fromAndroidParams = event;
    });
  }

  void _onFromAndroidError(Object error) {
    setState(() {
      _fromAndroidParams = "error";
      print(error);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _startFromAndroidPlugin();
  }

  //用于从Android原生获取参数

  //带参数跳回Android界面
  static const toAndroidPlugin =
      const MethodChannel('com.litngzhe.toandroid/plugin');

  Future<Null> _jumpToNative() async {
    String result = await toAndroidPlugin.invokeMethod('withoutParams');
    print(result);
  }

  Future<Null> _jumpToNativeWithParams() async {
    Map<String, String> map = {"flutter": "这是一条来自flutter的参数"};
    String result = await toAndroidPlugin.invokeMethod('withParams', map);
    print(result);
  }

  //带参数跳回Android界面

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Padding(
              padding:
                  const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
              child: new RaisedButton(
                  textColor: Colors.black,
                  child: new Text('跳转到原生界面'),
                  onPressed: () {
                    _jumpToNative();
                  }),
            ),
            new Padding(
              padding:
                  const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
              child: new RaisedButton(
                  textColor: Colors.black,
                  child: new Text('跳转到原生界面(带参数)'),
                  onPressed: () {
                    _jumpToNativeWithParams();
                  }),
            ),
            new Padding(
              padding:
                  const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
              child: new Text('这是一个从原生获取的参数：$_fromAndroidParams'),
            ),
            Text(
              'flutter自己的动作:$_counter',
              style: Theme.of(context).textTheme.displaySmall,
            ),
            ElevatedButton(
              child: Text("跳到知乎首页"),
              onPressed: () {
                Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (context) => new Scaffold(
                          appBar: new AppBar(
                            title: new Text("知乎练习首页"),
                          ),
                          body: new Zhihu(),
                        )));
              },
            ),
            ElevatedButton(
              child: Text("跳到pos"),
              onPressed: () {
                Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (context) => new Scaffold(
                          appBar: new AppBar(
                            title: new Text("pos练习首页"),
                          ),
                          body: new PosApp(),
                        )));
              },
            ),
            ElevatedButton(
              child: Text("跳到基础学习"),
              onPressed: () {
                Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (context) => new Scaffold(
                          appBar: new AppBar(
                            title: new Text("基础练习首页"),
                          ),
                          body: new BaseLearn(),
                        )));
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
